package org.erp.admins.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.erp.admins.infrastructure.convert.PaymentChannelsConvert;
import org.erp.admins.domain.entity.PaymentChannels;
import org.erp.admins.domain.dto.PaymentChannelsDto;
import org.erp.admins.domain.vo.PaymentChannelsVo;
import org.erp.admins.infrastructure.mapper.PaymentChannelsMapper;
import org.erp.admins.service.PaymentChannelsService;
import org.erp.common.page.PageQueryModel;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("paymentChannelsService")
public class PaymentChannelsServiceImpl extends ServiceImpl<PaymentChannelsMapper, PaymentChannels> implements PaymentChannelsService {

    @Resource
    private PaymentChannelsConvert paymentChannelsConvert;


    @Override
    public PageQueryModel<PaymentChannelsVo> getListByPage(PaymentChannelsDto dto) {
        LambdaQueryWrapper<PaymentChannels> wrapper = buildListQuery(dto);
        Page<PaymentChannels> page = this.page(dto.buildPage(), wrapper);
        List<PaymentChannelsVo> voList = page.getRecords().stream()
                .map(paymentChannelsConvert::entityToVo).collect(Collectors.toList());
        return PageQueryModel.of(page.getPages(), voList);
    }

    @Override
    public boolean updPaymentChannels(PaymentChannelsDto dto) {
        Assert.notNull(dto.getId(), "ID不能为空");
        Assert.isTrue(StringUtils.hasText(dto.getChannelName()), "渠道名称不能为空");
        Assert.isTrue(!existsByName(dto.getChannelName(), dto.getId()), "渠道名称已存在");
        PaymentChannels entity = paymentChannelsConvert.dtoToEntity(dto);
        return this.updateById(entity);
    }

    @Override
    public boolean addPaymentChannels(PaymentChannelsDto dto) {
        Assert.isTrue(StringUtils.hasText(dto.getChannelName()), "渠道名称不能为空");
        Assert.isTrue(!existsByName(dto.getChannelName(), null), "渠道名称已存在");

        PaymentChannels entity = paymentChannelsConvert.dtoToEntity(dto);
        entity.setDelFlag(0);
        return this.save(entity);
    }

    @Override
    public PaymentChannelsVo getPaymentChannelsVoById(Long id) {
        PaymentChannels entity = this.getById(id);
        if (entity != null) {
            return paymentChannelsConvert.entityToVo(entity);
        }
        return null;
    }

    @Override
    public List<PaymentChannelsVo> getPaymentChannelsVoList(PaymentChannelsDto dto) {
        LambdaQueryWrapper<PaymentChannels> wrapper = buildListQuery(dto);
        List<PaymentChannels> list = this.list(wrapper);
        return list.stream().map(paymentChannelsConvert::entityToVo).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<PaymentChannels> buildListQuery(PaymentChannelsDto dto) {
        LambdaQueryWrapper<PaymentChannels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(dto.getChannelName()), PaymentChannels::getChannelName, dto.getChannelName())
                .eq(PaymentChannels::getDelFlag, 0)
                .orderByDesc(PaymentChannels::getCreateTime);
        if (StringUtils.hasText(dto.getStatus())) {
            wrapper.eq(PaymentChannels::getStatus, dto.getStatus());
        }
        return wrapper;
    }

    private boolean existsByName(String channelName, Long excludeId) {
        LambdaQueryWrapper<PaymentChannels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentChannels::getChannelName, channelName)
                .eq(PaymentChannels::getDelFlag, 0);
        if (excludeId != null) {
            wrapper.ne(PaymentChannels::getId, excludeId);
        }
        return this.count(wrapper) > 0;
    }
}

